package com.zhouyu.tool;

import com.zhouyu.service.OrderService;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class ZhouyuBeanPostProcesser implements InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor,BeanPostProcessor {

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			for (Field field : bean.getClass().getDeclaredFields()) {
				if (field.isAnnotationPresent(ZhouyuInject.class)) {
					field.setAccessible(true);
					try {
						field.set(bean, "123");
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return pvs;
	}

	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
		if ("userService".equals(beanName)) {
			System.out.println("实例化后合并beanDefinition");
			beanDefinition.getPropertyValues().add("orderService", new OrderService());
		}

		if("destroyBean".equals(beanName)){
			System.out.println(beanName +" 设置销毁的方法");
			beanDefinition.setDestroyMethodName(AbstractBeanDefinition.INFER_METHOD);
		}

	}

	@Override
	public void resetBeanDefinition(String beanName) {
		MergedBeanDefinitionPostProcessor.super.resetBeanDefinition(beanName);
	}

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if(beanName.equals("userService")){
			System.out.println("实例化前");
			//注意：如果此处返回则后面直接会跳到 初始化后这一步骤
//			return new UserService();
		}
		if(beanName.equals("injectAutowiredBean")){
			System.out.println("实例化前"+ beanName);
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if(beanName.equals("userService")){
			System.out.println("实例化后");
		}
		if(beanName.equals("injectAutowiredBean")){
			System.out.println("实例化后"+ beanName);
		}
		return true;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(beanName.equals("userService")){
			System.out.println("初始化前:" + bean);
		}

		if(beanName.equals("injectAutowiredBean")){
			System.out.println("初始化前："+bean);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(beanName.equals("userService")){
			System.out.println("初始化后:" + bean);
		}
		return bean;
	}
}
